import axios from "axios";
import { ElMessage } from "element-plus";
if (process.env.NODE_ENV == "development") {
  axios.defaults.baseURL = process.env.VUE_APP_API;
} else {
  axios.defaults.baseURL = process.env.VUE_APP_API;
}
axios.interceptors.response.use(
  (response: any): any => {
    // 登录
    if (response.config.params.dl == "登录") {
      if (response.statusText == "OK") {
        if (response.data.length) {
          ElMessage({
            message: "登入成功",
            type: "success",
          });
          return response;
        } else {
          ElMessage.error("账号不存在或密码错误");
          return false;
        }
      }
    }
    //注册
    if (response.statusText == "Created") {
      if (response.data.id) {
        ElMessage({
          message: "注册成功",
          type: "success",
        });
        return response;
      } else {
        ElMessage.error("注册失败");
        return false;
      }
    }
    //住户列表
    if (response.statusText == "OK" && response.config.params.zh == "住户列表")
      if (response.data.length) {
        ElMessage({
          message: "获取住户列表",
          type: "success",
        });
        return response;
      } else {
        ElMessage.error("获取住户列表失败");
        return false;
      }
    if (
      response.statusText == "OK" &&
      response.config.params.zh == "住户列表长度"
    )
      if (response.data.length) {
        return response.data.length;
      } else {
        ElMessage.error("获取住户列表失败");
        return false;
      }

    if (response.config.method == "put") {
      ElMessage({
        message: "修改成功",
        type: "success",
      });
      return response.data;
    }
    if (response.config.method == "delete") {
      ElMessage({
        message: "删除成功",
        type: "success",
      });
      return true;
    }
    return response;
  },
  function (error) {
    switch (error.response.status) {
      case 404:
        ElMessage.error("当前路径有误！！");
        break;
      case 500:
        ElMessage.error("服务器连接失败请稍后再试！！");
        break;
      default:
        ElMessage.error("未知错误!!!");
        break;
    }
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);
export default axios;
